#
# TCSS 422 - Spring 2013
# Paul Ganea, Ralph Feltis, Tarl Hahn
# Team: Something Awesome (We can't remember the exact name)
# 5/2/2013
#

#
# Single threaded implementation of a web crawler
#
import threading
from AbstractCrawler import AbstractCrawler
        
class SingleThreadedCrawler(AbstractCrawler):
    
    def __init__(self):
        
        super(SingleThreadedCrawler, self).__init__()
    
    def Crawl(self, seedURL):
        
        self.isCrawling = False
        t = threading.Thread(target=lambda: self.DoSingleThreadedCrawl(seedURL), name="SingleThreadedCrawler")
        t.daemon = True
        super(SingleThreadedCrawler, self).getLog().put("Spinning up " + t.getName(), True, None)
        t.start()
            
    def DoSingleThreadedCrawl(self, seedURL):
        super(SingleThreadedCrawler, self).StartCrawling()        

        unparsedPages = super(SingleThreadedCrawler, self).getUnparsedPages()
        unprocessedURLs = super(SingleThreadedCrawler, self).getUnprocessedURLs()
        super(SingleThreadedCrawler, self).enqueueURL(seedURL)
        while not unparsedPages.empty() or not unprocessedURLs.empty():
            if not unprocessedURLs.empty():
                super(SingleThreadedCrawler, self).RetrieveURL()
            if not unparsedPages.empty():
                super(SingleThreadedCrawler, self).ParsePage()